package basicservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type FinanceAccountSubjectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFinanceAccountSubjectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FinanceAccountSubjectAddLogic {
	return &FinanceAccountSubjectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// FinanceAccountSubjectAdd 创建财务科目表项
func (l *FinanceAccountSubjectAddLogic) FinanceAccountSubjectAdd(in *property_service.FinanceAccountSubject) (*property_service.ResponseId, error) {
	// 获取财务科目表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).FinanceAccountSubject
	// 创建一个新的财务科目表模型对象，不包含ID、TenantID、CreatedAt、UpdatedAt字段
	mod := &model.FinanceAccountSubject{
		ParentID:         in.ParentID,         // 父级ID
		ParentCode:       in.ParentCode,       //父级编码
		ParentLs:         in.ParentLs,         // 父级列表
		SubjectCode:      in.SubjectCode,      // 科目编码
		SubjectName:      in.SubjectName,      // 科目名称
		BalanceDirection: in.BalanceDirection, // 余额方向
		SubjectID:        in.SubjectID,        // 科目类型id
		SubjectType:      in.SubjectType,      // 科目类型
		TenantID:         in.TenantID,         // 租户ID
		CreatedAtuser:    in.CreatedAtuser,    // 创建人
		UpdatedAtuser:    in.UpdatedAtuser,    // 更新人
	}
	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		res, err := result.Err(err).Ed()
		return l.buildResponse(mod.ID, res, err)
	}
	// 创建成功，返回成功响应
	res, err := result.Suc().AddEd()
	return l.buildResponse(mod.ID, res, err)
}

// 在 ContractMainAddLogic 结构体中添加辅助方法
func (l *FinanceAccountSubjectAddLogic) buildResponse(id int64, res *property_service.Response, err error) (*property_service.ResponseId, error) {
	return &property_service.ResponseId{
		Id:      id,
		Code:    res.Code,
		Message: res.Message,
	}, err
}
